//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client.creation;

import java.util.HashSet;
import java.util.Set;

public final class CheckerMojiPanel extends Checker {

	public CheckerMojiPanel(PanelProblemCreation panelProblemCreation) {
		super(panelProblemCreation);
	}

	protected boolean check(WidgetProblemForm form) {
		boolean result = super.check(form);

		int numberOfAnswer = form.getNumberOfAnswers();
		if (numberOfAnswer == 0) {
			result = false;
			warn("解答が入力されていません");
		}

		for (int answerIndex = 0; answerIndex < numberOfAnswer; ++answerIndex) {
			final String answer = form.getTextBoxAnswer(answerIndex);
			if (answer.length() < 3 || 6 < answer.length()) {
				result = false;
				warn((answerIndex + 1) + "番目の解答が3～6文字になっていません");
			}

			if (form.getTextBoxAnswer(0).length() != answer.length()) {
				result = false;
				warn("解答の長さがそろっていません");
			}
		}

		if (form.getNumberOfChoice() < 1) {
			result = false;
			warn("選択文字群が入力されていません");
		}
		final String choice = form.getTextBoxChoice(0);
		if (form.getTextBoxAnswer(0).length() == 3) {
			if (choice.length() != 6) {
				result = false;
				warn("解答が3文字の場合は選択文字群には6文字必要です");
			}
		} else if (4 <= form.getTextBoxAnswer(0).length() && form.getTextBoxAnswer(0).length() <= 6) {
			if (choice.length() != 10) {
				result = false;
				warn("解答が4～6文字の場合は選択文字群には10文字必要です");
			}
		}

		final Set<String> letters = createStringLetterSet(choice);
		if (letters.size() != choice.length()) {
			result = false;
			warn("選択文字群に同じ文字が含まれています");
		}

		if (form.getNumberOfChoice() > 0 && numberOfAnswer > 0) {
			final Set<String> choiceSet = createStringLetterSet(form.getTextBoxChoice(0));

			for (int answerIndex = 0; answerIndex < numberOfAnswer; ++answerIndex) {
				final String answer = form.getTextBoxAnswer(answerIndex);

				final Set<String> answerSet = createStringLetterSet(answer);
				for (String answerLetter : answerSet) {
					if (!choiceSet.contains(answerLetter)) {
						result = false;
						warn((answerIndex + 1) + "番目の解答の文字が選択文字群に含まれていません");
						break;
					}
				}
			}
		}

		return result;
	}

	private Set<String> createStringLetterSet(final String s) {
		final Set<String> letters = new HashSet<String>();
		for (int i = 0; i < s.length(); ++i) {
			letters.add(s.substring(i, i + 1));
		}
		return letters;
	}
}
